/*---------------------------------------------------------------------------
    $Workfile		: AndroidDotNerManager.h $

    Author			: Joseph Chacko, Ajith Joseph
    Last $Modtime	: 16/11/2011 
    Last $Revision	: 1 $
    $History		: AndroidDotNerManager.h $
 ---------------------------------------------------------------------------*/

#pragma once
using namespace System;
using namespace::System::IO;
using namespace Microsoft::Win32;

namespace AndroidDotNET
{
#define ANDROID_DOT_NET_DEFAULT_SETTINGS_FILE "AndroidDotNetSettings.xml"

	public ref class AndroidTargetSettings
	{
	public:
		System::String^ m_strTargetName;
		System::String^ m_strPlatform;
		System::String^ m_strAPI;
		System::String^ m_strFolderName;
		System::Collections::Generic::List<AndroidTargetSettings^>^ m_NewAndroidTSArray;
	
		System::String^ GetAndroidcmdLineParms()
		{
			return "display sdk";
		}
		AndroidTargetSettings()
		{
			m_strTargetName="";
			m_strPlatform="";
			m_strAPI="";
			m_strFolderName="";
			m_NewAndroidTSArray=gcnew System::Collections::Generic::List<AndroidTargetSettings^>();
		}
		bool LoadAllAndroidTargetPlatforms(System::String^ strSDKLocation)
		{
			array<System::String^>^ SubDirs= Directory::GetDirectories(strSDKLocation+"\\platforms");
			for(int i=0;i<SubDirs->Length;i++)
			{
				AndroidTargetSettings^ TargetSettings=gcnew AndroidTargetSettings();
		
				//Get the folder name
				array<Char>^Folderchars = {'\\'};
				array<String^>^ FolderTokens =SubDirs[i]->Split(Folderchars);
				TargetSettings->m_strFolderName=FolderTokens[FolderTokens->Length-1];

				//To get the content of each directory
				System::String^ strFileContent=File::ReadAllText(SubDirs[i]+"\\Source.Properties");
				array<Char>^chars = {'\n'};
				array<String^>^ Tokens =strFileContent->Split(chars);
				for(int j=0;j<Tokens->Length;j++)
				{
					if(Tokens[j]->Contains("AndroidVersion.ApiLevel"))
					{
						String^ strAPIInfo=Tokens[j];
						array<Char>^chars = {'='};	
						array<String^>^ APITokens=strAPIInfo->Split(chars);
						TargetSettings->m_strAPI=APITokens[1];
					}
					else if(Tokens[j]->Contains("Platform.Version"))
					{
						String^ VersionInfo=Tokens[j];
						array<Char>^chars = {'='};	
						array<String^>^ VersionTokens=VersionInfo->Split(chars);
						TargetSettings->m_strPlatform=VersionTokens[1];
						TargetSettings->m_strTargetName="Android "+TargetSettings->m_strPlatform;
					}
					else if(Tokens[j]->Contains("{"))
					{
					
					}
				}
				m_NewAndroidTSArray->Add(TargetSettings);
			}
			return 0;
		}
	};

	public ref class NewAndroidProjectInfo
	{
	public:
		System::String^ m_strProjectName;
		System::String^ m_strLocation;
		System::String^ m_strActivityName;
		System::String^ m_strPackageName;
		bool m_bCopySrcFromExistingSample;
		System::String^ m_strSampleName;
		System::String^ m_strName;
		System::String^ m_strValue;
		System::String^ m_strExistingLocation;
		AndroidTargetSettings^ m_SelectedTargetSettings;
		System::Collections::Generic::List<AndroidTargetSettings^>^ m_NewAndroidTSArray;
		System::String^ location;
		
		virtual System::String^ ToString() override 
		{
			return m_strName;
		}
		System::String^ GetAndroidCreateCmdLineParms(System::String^ strTarget)
		{
			return "create project --target "+ strTarget +" --name "+ m_strProjectName + " --package "+m_strPackageName+" --activity "+m_strActivityName+" --path \""+m_strLocation+"\"\\"+m_strProjectName;
		}
		System::String^ GetAndroidUpdateCmdLineParms(System::String^ strTarget)
		{
			return  "update project --target "+ strTarget +" --name "+m_strName + " --path "+m_strValue +"";
		}
		
		System::String^ GetAndroidCompileCmdLineParms()
		{
			return "ant";
		}
		NewAndroidProjectInfo()
		{
			m_strName="";
			m_strProjectName="";
			m_strActivityName="";
			m_strPackageName="";	
			m_NewAndroidTSArray= gcnew System::Collections::Generic::List<AndroidTargetSettings^>();
			m_SelectedTargetSettings=gcnew AndroidTargetSettings();
		}
	};
[Serializable]
public ref class CAndroidDotNetSettings
{
public:
	System::String^ m_strSDKLocation;
	System::String^ m_strNewAvdDevice;
	AndroidTargetSettings^ m_SelectedTarget;
	System::Collections::Generic::List<AndroidTargetSettings^>^ m_AndroidTSArray;
	System::String^ GetAndroidCmdLineParms()
	{
		return "";
	}
	CAndroidDotNetSettings()
	{
		m_AndroidTSArray= gcnew System::Collections::Generic::List<AndroidTargetSettings^>();
		m_SelectedTarget= gcnew AndroidTargetSettings();
		m_strSDKLocation=LoadSDKLocationFromRegistry();
	}

	//Retrive previous SDK location from registry
private: System::String^ LoadSDKLocationFromRegistry()
		 {
			 try
			 {
				 RegistryKey^ key = Registry::CurrentUser->CreateSubKey("Software\\Tek-Tips.Info\\Android\\");
				 m_strSDKLocation= key->GetValue("DEFAULT_SDK_LOCATION")->ToString();
				 key->Close();
				 return m_strSDKLocation;
			 }
			 catch (...)
			 {
				 return "";
			 }
		 }
};

ref class CAndroidDotNetManager
{
	System::String ^ m_CurrentProjectPath;
	CAndroidDotNetSettings^ m_AndroidDotNetSettings;
	NewAndroidProjectInfo^  m_NewAndroidProjectInfo; 
	AndroidTargetSettings^  m_AndroidTargetSettings;
	
    EnvDTE80::DTE2^ m_DTE2;
	System::Collections::Generic::List<AndroidTargetSettings^>^ m_NewAndroidTSArray;
public:
	CAndroidDotNetManager()
	{
		m_NewAndroidTSArray= gcnew System::Collections::Generic::List<AndroidTargetSettings^>();
	}
	
	CAndroidDotNetSettings^ GetAndroidSDKSettings()
	{
		return m_AndroidDotNetSettings;
	}
	
	CAndroidDotNetManager(EnvDTE80::DTE2^ DTE2);
	bool InvokeNewAndroidProject(void);
	bool InvokeNewAndroidProjectOpenMenu();
	bool InvokeNewAndroidProjectOpenFolder();
	// Invoke Anrdoid SDK Compile to compile this project
	int InvokeCompileCommand();
	int InvokeRunCommand(void);
	int InvokeDebugCommand(void);
	int InvokeSettingsCommand(void);
	int InvokeAndroidEmulatorCommand(void);
	int InvokeNewEmulatorCommand(void);
	int InvokeAboutAndroidCommand(void);
	
	bool LoadAndroidSDKSettings(void);
	bool SaveAndroidSettings(void);
	bool LoadAndDisplayInSolutionExplorer(System::String^ OpenFolderLoad);
	bool CreateAndDisplayProjectInSolutionExplorer(System::String^ OpenFolderLoad);
	bool CreateProjectInSolutionExplorer(System::String^ OpenFolderLoad);
	System::String^ GetAssemblyDirectory(void);
	bool LoadAllAndroidTargetPlatforms(System::String^ strSDKLocation);
};
public enum CREATE_PROJECT_OPTION{CREATE_PROJECT_FROM_EXISTING_SOURCE,CREATE_NEW_PROJECT};
}
